package com.joolun.activity.service.impl;

import java.util.List;
import java.util.Locale;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joolun.activity.domain.ActivityAward;
import com.joolun.activity.domain.ActivityAwardInfo;
import com.joolun.activity.domain.ActivityTakeInfo;
import com.joolun.activity.mapper.ActivityAwardMapper;
import com.joolun.activity.service.IActivityAwardInfoService;
import com.joolun.activity.service.IActivityAwardService;
import com.joolun.activity.service.IActivityTakeInfoService;
import com.joolun.common.utils.DateUtils;
import com.joolun.common.utils.GeneratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.joolun.activity.mapper.ActivityInfoMapper;
import com.joolun.activity.domain.ActivityInfo;
import com.joolun.activity.service.IActivityInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动Service业务层处理
 *
 * @author www.joolun.com
 * @date 2022-11-22
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements IActivityInfoService
{
    @Autowired
    private ActivityInfoMapper activityInfoMapper;
    @Autowired
    IActivityAwardService iActivityAwardService;
    @Autowired
    IActivityTakeInfoService iActivityTakeInfoService;

    /**
     * 查询活动
     *
     * @param id 活动ID
     * @return 活动
     */
    @Override
    public ActivityInfo selectActivityInfoById(String id)
    {
        return activityInfoMapper.selectActivityInfoById(id);
    }

    /**
     * 查询活动列表
     *
     * @param activityInfo 活动
     * @return 活动
     */
    @Override
    public List<ActivityInfo> selectActivityInfoList(ActivityInfo activityInfo)
    {
        return activityInfoMapper.selectActivityInfoList(activityInfo);
    }

    /**
     * 新增活动
     *
     * @param activityInfo 活动
     * @return 结果
     */
    @Override
    public int insertActivityInfo(ActivityInfo activityInfo)
    {
        activityInfo.setStatus("1");
        activityInfo.setNum(0L);
        activityInfo.setActivityCode(GeneratorUtils.getSecurityCode(5, GeneratorUtils.SecurityCodeLevel.Hard,false).toUpperCase());
        activityInfo.setCreateTime(DateUtils.getNowDate());
        save(activityInfo);
        return 1;
    }

    /**
     * 修改活动
     *
     * @param activityInfo 活动
     * @return 结果
     */
    @Override
    public int updateActivityInfo(ActivityInfo activityInfo)
    {
        activityInfo.setUpdateTime(DateUtils.getNowDate());
        return activityInfoMapper.updateActivityInfo(activityInfo);
    }

    /**
     * 批量删除活动
     *
     * @param ids 需要删除的活动ID
     * @return 结果
     */
    @Override
    public int deleteActivityInfoByIds(String[] ids)
    {
        return activityInfoMapper.deleteActivityInfoByIds(ids);
    }

    /**
     * 删除活动信息
     *
     * @param id 活动ID
     * @return 结果
     */
    @Override
    public int deleteActivityInfoById(String id)
    {
        return activityInfoMapper.deleteActivityInfoById(id);
    }

    @Override
    @Transactional
    public int removeAward(String[] ids) {
        //查询活动状态
        ActivityInfo activityInfo=getById(ids[0]);
        if(!activityInfo.getStatus().equals("3")){
            throw new RuntimeException("活动非已结束状态不能进行此操作");
        }
        iActivityAwardService.update(new UpdateWrapper<ActivityAward>()
                .set("del_flag","1")
                .eq("activity_code",activityInfo.getActivityCode()));
        return 1;
    }

    @Override
    public int initTakeInfo(String[] ids) {
        //判断活动是否开启
        ActivityInfo activityInfo=getById(ids[0]);
        if(!activityInfo.getStatus().equals("3")){
            throw new RuntimeException("活动非已结束状态不能进行此操作");
        }
        iActivityTakeInfoService.update(new UpdateWrapper<ActivityTakeInfo>()
                .set("status","2")
                .eq("status","3")
                .eq("activity_code",activityInfo.getActivityCode())
        );
        return 1;
    }
}
